import Vue from "vue";
import Router from "vue-router";
import home from "./home";
import nofound from "./404";
import service from "@u/request.js";
Vue.use(Router);

//添加页面路由
const routes = [...nofound, ...home];

// add route path
routes.forEach(route => {
  route.path = route.path || "/" + (route.name || "");
});

//路由配置
const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  props: true,
  routes
});

//路由前置守卫
router.beforeEach((to, from, next) => {
  // 把上个页面还没结束的请求取消掉
  service.forEach(item => {
    item.routeChangeCancel && item.cancel();
  });
  //更改页面标题
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  next();
});

export { router };
